#include <iostream>
using namespace std;
void funcSwitch(int a, int b, char c) {
  switch (c) {
    case '+':
      cout << a + b << endl;
      break;
    case '-': 
      cout << a - b << endl;
      break;
    case '*':
      cout << a * b << endl;
      break;
    case '/':
      if (b == 0) cout << "Divided by zero!" << endl;
      else cout << a / b << endl;
      break;
    default:
      cout << "Invalid operator!" << endl;
  }
}
void funcIf(int a, int b, char c) {
  if (c == '+') cout << a + b << endl;
  else if (c == '-') cout << a - b << endl;
  else if (c == '*') cout << a * b << endl;
  else if (c == '/') {
    if (b == 0) cout << "Divided by zero!" << endl;
    else cout << a / b << endl;
  } else {
    cout << "Invalid operator!" << endl;
  }
}
void funcSmallSwitch(int c) {
  int x;
  switch (c) {
    case 0:
      x = 1;
      break;
    case 1:
      x = 10;
      break;
    case 2:
      x = 3;
      break;
    case 5:
      x = 4;
      break;
    default:
      x = 8;
  }
  cout << x << endl;
}
void funcSmallIf(int c) {
  int x;
  if (c == 0) x = 1;
  else if (c == 1) x = 10;
  else if (c == 2) x = 3;
  else if (c == 5) x = 5;
  else x = 8;
  cout << x << endl;
}
int main() {
  cout << "不需要使用 cr 来运行这段代码，而是使用以下指令生成汇编代码" << endl;
  cout << "c++ switch_vs_if.cpp -S -O2" << endl;
  cout << "生成的文件 switch_vs_if.s 就是汇编代码，可以直接在 VSCode 中打开" << endl;
  cout << "在打开的汇编文件中搜索 funcSwitch, funcIf 等名字就可以快速定位对应的代码" << endl;
  cout << endl;
  cout << "仔细观察会发现，case 比较小的情况下，if 也会被优化为哈希表" << endl;
  cout << "也可以去掉编译选项 -O2，查看不加 O2 优化的结果，来了解 O2 优化到底优化了什么" << endl;
  cout << endl;
  cout << "注：不同电脑芯片生产的汇编代码并不相同，比如苹果电脑 ARM 架构芯片的汇编指令会和视频相差很大，但大体逻辑是相同的" << endl;
}